package cn.sccl.dirms.topo.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.util.StrUtils;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.topo.discovery.model.DeviceBaseInfo;
import cn.sccl.dirms.topo.discovery.model.DeviceInfo;
import cn.sccl.dirms.topo.model.DiscoveryResult;
import cn.sccl.dirms.topo.service.DiscoveryResultManager;
import cn.sccl.dirms.topo.snmp.mib.IfEntry;
import cn.sccl.dirms.topo.snmp.mib.IpNetToMediaEntry;
import cn.sccl.dirms.topo.snmp.mib.IpRouteEntry;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 存储拓扑发现结果，以json格式存储，每次发现存储一次记录 的Controller
 * Created by julei on 2017-10-17.
 */
@RestController
@RequestMapping(value = "/nms/topo/discoveryResult")
public class DiscoveryResultController extends BaseController {

    @Autowired
    private DiscoveryResultManager discoveryResultManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<DiscoveryResult> listAllDiscoveryResult() {
        List<DiscoveryResult> a = discoveryResultManager.listAll();
        return a;
    }
    /**
     * 获取设备基础信息
     * @param macIp
     * @return
     */
    @RequestMapping(value = "/getDeviceBase", method = RequestMethod.GET)
    public DeviceBaseInfo getDeviceBase(@RequestParam String macIp) {
        DeviceInfo deviceInfo = getDevice(macIp);
        DeviceBaseInfo baseInfo = new DeviceBaseInfo(deviceInfo);
        return baseInfo;
    }


    /**
     * 获取端口列表信息
     * @param macIp
     * @return
     */
    @RequestMapping(value = "/getInterface", method = RequestMethod.GET)
    public List<IfEntry> getInterface(@RequestParam String macIp) {
        DeviceInfo deviceInfo = getDevice(macIp);
        List<IfEntry> ifEntryList = deviceInfo.getIfEntries();
        return ifEntryList;
    }
    /**
     * 获取指定端口信息
     * @param macIp
     * @param ifIndex
     * @return
     */
    @RequestMapping(value = "/getIfEntry", method = RequestMethod.GET)
    public IfEntry getIfEntry(@RequestParam String macIp, @RequestParam int ifIndex) {
        DeviceInfo deviceInfo = getDevice(macIp);
        List<IfEntry> ifEntryList = deviceInfo.getIfEntries();
        for (IfEntry ifEntry : ifEntryList) {
            if (ifEntry.getIfIndex() == ifIndex) {
                return ifEntry;
            }
        }
        return new IfEntry();
    }

    /**
     * 获取转发表信息
     * @param macIp
     * @return
     */
    @RequestMapping(value = "/getArp", method = RequestMethod.GET)
    public List<IpNetToMediaEntry> getArp(@RequestParam String macIp) {
        DeviceInfo deviceInfo = getDevice(macIp);
        List<IpNetToMediaEntry> IpNetToMediaEntrys = deviceInfo.getIpNetToMediaEntries();
        return IpNetToMediaEntrys;
    }

    /**
     * 获取路由表信息
     * @param macIp
     * @return
     */
    @RequestMapping(value = "/getRouter", method = RequestMethod.GET)
    public List<IpRouteEntry> getRouter(@RequestParam String macIp) {
        DeviceInfo deviceInfo = getDevice(macIp);
        List<IpRouteEntry> IpRouteEntrys = deviceInfo.getIpRouteEntries();
        return IpRouteEntrys;
    }

    /**
     * 获取设备信息
     * @param macIp
     * @return
     */
    private DeviceInfo getDevice(String macIp) {
        List<DiscoveryResult> all = discoveryResultManager.listAll();
        DiscoveryResult recentResult = all.get(all.size() - 1);
        List<DeviceInfo> deviceInfoList = StrUtils.bytesToObject(recentResult.getDeviceList());
        for (DeviceInfo deviceInfo : deviceInfoList) {
            if (deviceInfo.getMark().equals(macIp)) {
                return deviceInfo;
            }
        }
        return new DeviceInfo();
    }
    /**
     * 分页查询，分页参数放置在url路径中
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<DiscoveryResult> pageQueryDiscoveryResult(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return discoveryResultManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询，分页参数放置在request中
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<DiscoveryResult> pageQueryDiscoveryResult(HttpServletRequest request) {
        return discoveryResultManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param discoveryResult
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public DiscoveryResult saveDiscoveryResult(@RequestBody DiscoveryResult discoveryResult) {
        discoveryResultManager.save(discoveryResult);
        return discoveryResult;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteDiscoveryResult(@PathVariable("id") String id) {
        discoveryResultManager.delete(id);
    }


}
